using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using Spread.Common.DataStore;
using FarPoint.Win.Spread.CellType;

namespace Spread.DataBinding
{
    public partial class SheetDataSource : BaseSampleControl
    {
        public SheetDataSource()
        {
            InitializeComponent();
            Name = "Sheet Data Source";

            var dataSource = new DataService().AccessDataSource;
            var data = dataSource.GetDataFrom("Nwind.mdb", "SELECT Products.ProductID, Products.ProductName, Suppliers.CompanyName, Categories.CategoryName, Products.QuantityPerUnit, Products.UnitPrice, Products.UnitsInStock, Products.UnitsOnOrder, Products.Discontinued FROM Suppliers INNER JOIN (Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID) ON Suppliers.SupplierID = Products.SupplierID ORDER BY Products.ProductID");

            //Default Binding
            _svDefaltBin.DataAutoSizeColumns = false;
            _svDefaltBin.SetColumnWidth(-1, 120);
            _svDefaltBin.DataSource = data;

            //Custom Binding
            _svCustomBin.DataAutoSizeColumns = false;
            _svCustomBin.SetColumnWidth(-1, 120);
            _svCustomBin.DataSource = data;
            _svCustomBin.BindDataColumn(2, "CategoryName");
            _svCustomBin.BindDataColumn(3, "CompanyName");
            _svCustomBin.BindDataColumn(4, "QuantityPerUnit");
            _svCustomBin.BindDataColumn(5, "UnitPrice");
            _svCustomBin.BindDataColumn(6, "Discontinued");
            _svCustomBin.BindDataColumn(7, "UnitsInStock");
            _svCustomBin.BindDataColumn(8, "UnitsOnOrder");

            _svCustomBin.Columns[0].Label = "ID";
            _svCustomBin.Columns[1].Label = "Name";
            _svCustomBin.Columns[2].Label = "Category";
            _svCustomBin.Columns[3].Label = "Supplier Company";
            _svCustomBin.Columns[4].Label = "Quantity";
            _svCustomBin.Columns[5].Label = "Unit Price";

            _svCustomBin.Columns[5].CellType = new CurrencyCellType();
            //Show title of sheet to make column names meaningful.
            _svCustomBin.TitleInfo = new FarPoint.Win.Spread.TitleInfo()
            {
                Text = "Products Detail",
                HorizontalAlign = FarPoint.Win.Spread.CellHorizontalAlignment.Center,
                Height = 20,
                BackColor = Color.DarkGreen,
                ForeColor = Color.White,
                Font = new Font("Segoi UI", 12, FontStyle.Bold),
                Visible = true,
            };
        }
    }
}


//
//SheetDataSource.Designer.cs
//


namespace Spread.DataBinding
{
    partial class SheetDataSource
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread();
            this._svCustomBin = new FarPoint.Win.Spread.SheetView();
            this._svDefaltBin = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this._svCustomBin)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this._svDefaltBin)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // fpSpread1
            // 
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this._svDefaltBin,
            this._svCustomBin});
            this.fpSpread1.Size = new System.Drawing.Size(477, 487);
            this.fpSpread1.TabIndex = 0;
            // 
            // _svCustomBin
            // 
            this._svCustomBin.Reset();
            this._svCustomBin.SheetName = "Custom Binding";
            // Formulas and custom names must be loaded with R1C1 reference style
            this._svCustomBin.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this._svCustomBin.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // _svDefaltBin
            // 
            this._svDefaltBin.Reset();
            this._svDefaltBin.SheetName = "Default Binding";
            // Formulas and custom names must be loaded with R1C1 reference style
            this._svDefaltBin.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this._svDefaltBin.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // DataBinding
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "DataBinding";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this._svCustomBin)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this._svDefaltBin)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView _svCustomBin;
        private FarPoint.Win.Spread.SheetView _svDefaltBin;
    }
}
